package thread;
/**
 * 例子：创建三个窗口卖票，总票数为100张
 * 继承Thread类的同步代码块
 *
 * @author Bboy Dday
 *  @create 2020-06-14--10:23
 */
public class WindowTest2 {

    public static void main(String[] args) {

        Window2 w1 = new Window2();
        Window2 w2 = new Window2();
        Window2 w3 = new Window2();

        w1.setName("窗口1");
        w2.setName("窗口2");
        w3.setName("窗口3");

        w1.start();
        w2.start();
        w3.start();


    }

}

class Window2 extends Thread{

    private static int ticket = 100;

    private static Object obj = new Object();

    @Override
    public void run(){

        while (true){

            //synchronized(obj) {
            synchronized ( Window2.class){
                    //错误的方式：this代表着t1,t2,t3三个对象
                    //synchronized (this){
                if (ticket > 0) {

                    try {
                        Thread.sleep( 100 );
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    System.out.println(getName() + " : " + "售票" + " : " + ticket);
                    ticket--;
                }
            }

        }

    }

}
